package cn.xpf.study.controller;

import cn.xpf.study.dao.LessonItemMapper;
import cn.xpf.study.dao.LessonMapper;
import cn.xpf.study.model.Lesson;
import cn.xpf.study.model.LessonItem;
import cn.xpf.study.model.LessonItemContent;
import cn.xpf.study.utils.JsonUtil;
import cn.xpf.study.utils.Test;
import java.util.LinkedList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Create on 2021/5/13
 *
 * @author shuxinsheng@qiyi.com
 **/
@RequestMapping("/lesson")
@Controller
public class LessonController {

    @Autowired
    LessonItemMapper lessonItemMapper;

    @Autowired
    LessonMapper lessonMapper;

    @RequestMapping("/index")
    public String index() {
        return "lesson1";
    }

    @RequestMapping("/detail")
    public String detail(Model model, @RequestParam("id") int lessonId) {
        Lesson lesson = lessonMapper.findById(lessonId);
        LessonItem lessonItem = lessonItemMapper.findOne(lessonId);
        List<LessonItemContent> contentList = null;
        if (lessonItem != null) {
            contentList = JsonUtil.fromContent(lessonItem.getContent());
        } else {
            contentList = new LinkedList<>();
        }
        model.addAttribute("lesson", lesson);
        model.addAttribute("contentList", contentList);
        return "lesson_demo";
    }


//    @RequestMapping("/test")
//    public String test(Model model) {
//        model.addAttribute("lesson", Test.s1);
//        return "lesson_demo";
//    }

}
